package com.coder.baseapp.mvp.base;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

import com.coder.baseapp.App;
import com.coder.baseapp.injecter.components.ActivityComponent;
import com.coder.baseapp.injecter.components.DaggerActivityComponent;
import com.coder.baseapp.injecter.module.ActivityModule;
import com.coder.baseapp.wrapper.AppStack;

import butterknife.ButterKnife;

/**
 * 基类Activity
 *
 * @Author: Tua.CoderGui
 * *
 * @Date: 2017/3/9
 * *
 * Copyright (c) 2017 All Rights Reserved.
 */
public abstract class BaseActivity extends AppCompatActivity implements ILayoutControl {

    private ActivityComponent mComponent;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        AppStack.getInstance().pushActivity(this);

        setContentView(getLayoutId());

        ButterKnife.bind(this);

        initComponent();

        initInjector(mComponent);

        initView(savedInstanceState);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        AppStack.getInstance().popActivity(this);
    }

    /**
     * 初始化component
     */
    protected void initComponent() {
        mComponent = DaggerActivityComponent.builder()
                .applicationComponent(App.getAppComponent())
                .activityModule(new ActivityModule(this))
                .build();
    }

    /**
     * Dagger 注入
     */
    protected abstract void initInjector(ActivityComponent component);


    @Override
    public void openActivity(Class<?> clazz) {
        openActivity(clazz, null);
    }

    @Override
    public void openActivity(Class<?> clazz, Bundle bundle) {
        Intent intent = new Intent(this, clazz);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }

    @Override
    public void openActivityWithResult(Class<?> clazz) {
        openActivityWithResult(clazz, RESULT_OK);
    }

    @Override
    public void openActivityWithResult(Class<?> clazz, int code) {
        openActivityWithResult(clazz, code, null);
    }

    @Override
    public void openActivityWithResult(Class<?> clazz, int code, Bundle bundle) {
        Intent intent = new Intent(this, clazz);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivityForResult(intent, code);
    }

    @Override
    public void showToastLong(String txt) {
        Toast.makeText(this, txt, Toast.LENGTH_LONG).show();
    }

    @Override
    public void showToastShort(String txt) {
        Toast.makeText(this, txt, Toast.LENGTH_SHORT).show();
    }
}
